
function Gizmo(name,mouseClickCallback,mouseOverCallback,mouseEnterCallback,mouseLeaveCallback)
{
  this.name = name;
  this.colour = null;
  
  this.mouseClickCallback = mouseClickCallback;
  this.mouseOverCallback = mouseOverCallback;
  this.mouseEnterCallback = mouseEnterCallback;
  this.mouseLeaveCallback = mouseLeaveCallback;
  
  this.mouseInside = false;
}


Gizmo.prototype.processMouseMove = function (point,colour)
{
  if(colour.a != 0)
    var newMouseInside = colour.compare(this.colour,true);
  else
    var newMouseInside = false;
  
  if(this.mouseInside)
  {
    if(newMouseInside)        // Was Inside Still Inside
    {
      if(this.mouseOverCallback != null)
        this.mouseOverCallback('Over',this.name,point);
    } else {                    // Was Inside now outside
      this.mouseInside = newMouseInside;
      if(this.mouseLeaveCallback != null)
        this.mouseLeaveCallback('Leave',this.name,point);
    }
  } else {
    if(newMouseInside)        // Was outside now Inside
    {
      this.mouseInside = newMouseInside;
      if(this.mouseEnterCallback != null)
        this.mouseEnterCallback('Enter',this.name,point);
    } else {                  // Was outside still outside
      // Winner?
    }
  }
}

Gizmo.prototype.processMouseClick = function (point,colour)
{
  if(colour.a == 0)
    return;
    
  if(this.mouseClickCallback != null && colour.compare(this.colour))
    this.mouseClickCallback('Click',name,point);
}

function BaseGizmos()
{
  this.gizmos = Array();
  this.currentGizmo = null;
}

BaseGizmos.prototype.addGizmo = function (gizmo)
{
  gizmo.colour = this.generateGizmoColour();
  this.gizmos.push(gizmo);
}

BaseGizmos.prototype.startGizmo = function (gizmo)
{
  for(var i in this.gizmos)
    if(this.gizmos[i].name == gizmo)
      this.currentGizmo = this.gizmos[i];
}

BaseGizmos.prototype.stopGizmo = function ()
{
  this.currentGizmo = null;
}

BaseGizmos.prototype.clearGizmos = function ()
{
  this.gizmos = Array();
  this.currentGizmo = null;  
}

BaseGizmos.prototype.hasGizmo = function ()
{
  return this.currentGizmo != null;
}

BaseGizmos.prototype.generateGizmoColour = function ()
{
  var chosenColour = 16 * this.gizmos.length;
  return rgb(chosenColour & 255,(chosenColour & (255 << 8)) >> 8,(chosenColour & (255 << 16)) >> 16);
}
